package com.woniuxy.three.pms.filter;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.SessionException;
import java.util.Locale;

/**
 * @ProjectName: Pms
 * @Package: com.woniuxy.three.pms.filter
 * @ClassName: SystemLogoutFilter
 * @Author: 12092
 * @Description: 登出时清除缓存 保证数据安全
 * @Date: 2019/6/27 19:57
 * @Version: 1.0
 */
@Slf4j
public class SystemLogoutFilter extends LogoutFilter {

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {

        //在这里执行退出系统前需要清空的数据

        Subject subject = this.getSubject(request, response);
        if (this.isPostOnlyLogout() && !WebUtils.toHttp(request).getMethod().toUpperCase(Locale.ENGLISH).equals("POST")) {
            return this.onLogoutRequestNotAPost(request, response);
        } else {
            subject.logout();
            HttpServletRequest req = (HttpServletRequest) request;
            String url = req.getRequestURI();
            String redirectUrl = "/html/login.html";
            if (url.contains("/user/logout")){
                redirectUrl = "/front/html/login.html";
            }else if (url.contains("/root/logout")){
                redirectUrl = "/hotel/html/login.html";
            }
            this.issueRedirect(request, response, redirectUrl);
            return false;
        }
    }
}
